package concurrent;

import basicType.CloneTest;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.*;
import java.util.concurrent.*;

/**
 * TODO: please加入描述
 * @author chenxin
 * @time 2017-05-06-20:27
 */
public class HashMapTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        List<MyObject> myObjectList = new ArrayList<>();
        myObjectList.add(new MyObject("a", 1));
        myObjectList.add(new MyObject("b", 2));

        Map<Integer, MyObject> map = Maps.newHashMap(Maps.uniqueIndex(myObjectList, new Function<MyObject, Integer>() {
            @Override
            public Integer apply(MyObject input) {
                return input.getFoo();
            }
        }));


        System.out.println(myObjectList.size());
        System.out.println(map.size());

        List<MyObject> myObjectList2 = new ArrayList<>(map.values());
        Collection<MyObject> myObjectList3 = map.values();
        map.remove(1);
        System.out.println(myObjectList2.size());
        System.out.println(myObjectList3.size());
    }

    static class MyObject {
        private String str;

        private int foo;

        public MyObject(String str, int foo) {
            this.str = str;
            this.foo = foo;
        }

        public int getFoo() {
            return foo;
        }

        public void setFoo(int foo) {
            this.foo = foo;
        }

        public String getStr() {
            return str;
        }

        public void setStr(String str) {
            this.str = str;
        }

        @Override
        public String toString() {
            return "MyObject{" +
                    "str:'" + str + '\'' +
                    ", foo:" + foo +
                    '}';
        }
    }


    private static void ttt() {
        Map<String, String> map = new HashMap<>();
        map.put(null, null);
        map.put("1", null);
        System.out.println(map.size());
        System.out.println(map.keySet());
        System.out.println(map.keySet().size());


//        Map<String, String> table = new Hashtable<>();
//        table.put(null, "");

        Map<String, String> empty = new HashMap<>();
        empty.put("k1", "v1");

        List<String> keys = new ArrayList<>(empty.keySet());
        System.out.println(empty.keySet());
        System.out.println(keys);


        System.out.println("end");
    }

    private static void testInit() {
        Map<String, String> map = new HashMap<>();

    }

    private static void testHashCode() {
        int lat = 22541476;
        int lng = 114103047;
        int hashCode = lat*1000000000 + lng;
        int x = lat * 1000000000;
        System.out.println(hashCode);
        System.out.println("test overflow x = " + x);
    }
}
